package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import modle.TaskFile;

/**
 * Data access object (DAO) for domain model class User.
 * 
 * @see hibernateDao.User
 * @author MyEclipse Persistence Tools
 */

public class TaskFileDao extends BaseDao {
	// property constants

	public int delete(TaskFile persistentInstance) {
		try {
			String sql = "delete from task_file where taskFileId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, persistentInstance.getTaskFileId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	public List<TaskFile> findAll() {
		try {
			String sql = "select * from task_file";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public TaskFile findById(java.lang.Integer id) {
		try {
			String sql = "select * from task_file where taskFileId = ?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, id);
			List<TaskFile> instanceList = this.getListByResultSet(ps
					.executeQuery());
			if (instanceList == null) {
				return null;
			}
			TaskFile instance = instanceList.get(0);
			return instance;
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	public List<TaskFile> findByProperty(String propertyName, Object value) {
		try {
			String sql = "select * from task where " + propertyName + "='"
					+ value + "'";
			PreparedStatement ps = this.getPreparedStatement(sql);
			return this.getListByResultSet(ps.executeQuery(sql));
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return null;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return null;
		}
	}

	@Override
	protected List<TaskFile> getListByResultSet(ResultSet rs) {
		// TODO Auto-generated method stub
		List<TaskFile> list = new ArrayList<TaskFile>();
		try {
			if (!rs.next()) {
				return null;
			}
			do {
				TaskFile taskFile = new TaskFile();
				TaskDao taskDao = new TaskDao();
				FileDao fileDao = new FileDao();
				taskFile.setTaskFileId(rs.getInt("taskFileId"));
				taskFile.setTask(taskDao.findById(rs.getInt("taskId")));
				taskFile.setFile(fileDao.findById(rs.getInt("fileId")));
				list.add(taskFile);
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		}
		return list;
	}

	public int save(TaskFile transientInstance) {
		try {
			String sql = "insert into task_file(taskId, fileId) values(?, ?)";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, transientInstance.getTask().getTaskId());
			ps.setInt(2, transientInstance.getFile().getFileId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}

	@Override
	protected void setSavePreparedStatement(PreparedStatement ps, Object obj)
			throws SQLException {
		// TODO Auto-generated method stub

	}

	public int update(TaskFile instance) {
		try {
			String sql = "update task_file set taskId=?, fileId=? where taskFileId=?";
			PreparedStatement ps = this.getPreparedStatement(sql);
			ps.setInt(1, instance.getTask().getTaskId());
			ps.setInt(2, instance.getFile().getFileId());
			return ps.executeUpdate();
		} catch (RuntimeException re) {
			JOptionPane.showMessageDialog(null, re.toString());
			return -1;
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.toString());
			return -1;
		}
	}
}